package com.mjfeed.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mjfeed.util.DBHelper;

/**
 * Dao基类
 * @author zhaominglei
 * @date 2014-12-11
 * 
 */
public class BaseDao {
	
	/**
	 * 获取记录数
	 */
	public int getCount(Context context, String tableName) {
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from "+tableName, null);
		Long count = null;
		if (cursor.moveToFirst()) {
			count = cursor.getLong(0);
		}
		cursor.close();
		return (count != null) ? count.intValue() : 0;
	}
	
	/**
	 * 保存
	 */
	public void save(Context context, String sql) {
		SQLiteDatabase db = DBHelper.getInstance(context).getWritableDatabase();
		db.execSQL(sql);
	}
	
	/**
	 * 清空表
	 */
	public void deleteAll(Context context, String tableName) {
		SQLiteDatabase  db = DBHelper.getInstance(context).getWritableDatabase();
		db.execSQL("delete from "+tableName);
	}
}
